<template>
    <div class="experience-management">
        <div class="header">
            <h2>经历管理</h2>
            <el-button v-permission="['editExperience']" type="primary" @click="showAddDialog">新增经历</el-button>
        </div>

        <div class="experience-list">
            <el-card v-for="(exp, index) in experiences" :key="index" class="experience-card"
                :class="{ disabled: exp.status === 0 }">
                <div class="card-content">
                    <div class="experience-info">
                        <div class="experience-date">
                            <el-tag type="info">{{ exp.date }}</el-tag>
                        </div>
                        <h3>{{ exp.name }}</h3>
                        <p class="experience-content">{{ exp.content }}</p>
                        <div class="status">
                            <el-tag :type="exp.status ? 'success' : 'info'">
                                {{ exp.status ? '启用' : '禁用' }}
                            </el-tag>
                        </div>
                    </div>
                </div>
                <div class="card-actions"  v-permission="['editExperience']">
                    <el-button size="small" @click="editExperience(index)">编辑</el-button>
                    <el-button size="small" :type="exp.status ? 'warning' : 'success'" @click="toggleStatus(index)">
                        {{ exp.status ? '禁用' : '启用' }}
                    </el-button>
                    <el-button size="small" type="danger" @click="deleteExperience(index)">删除</el-button>
                </div>
            </el-card>
        </div>

        <!-- 新增/编辑对话框 -->
        <el-dialog :title="dialogTitle" v-model="dialogVisible" width="50%">
            <el-form :model="currentExperience" label-width="100px">
                <el-form-item label="经历日期">
                    <el-date-picker v-model="currentExperience.date" type="date" placeholder="选择日期"
                        value-format="YYYY-MM-DD" />
                </el-form-item>
                <el-form-item label="经历名称">
                    <el-input v-model="currentExperience.name"></el-input>
                </el-form-item>
                <el-form-item label="经历内容">
                    <el-input v-model="currentExperience.content" type="textarea" :rows="4"></el-input>
                </el-form-item>
                <el-form-item label="状态">
                    <el-switch v-model="currentExperience.status" active-value="1" inactive-value="0"></el-switch>
                </el-form-item>
            </el-form>
            <template #footer>
                <el-button @click="dialogVisible = false">取消</el-button>
                <el-button type="primary" @click="saveExperience">保存</el-button>
            </template>
        </el-dialog>
    </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import { getExperienceList, addExperience, updateExperience, delExperience } from '@/api/experience'

const experiences = ref([])
const loading = ref(false)

// 获取经历列表
const fetchExperiences = async () => {
    try {
        loading.value = true
        const res = await getExperienceList()
        experiences.value = res.data.map(item => ({
            id: item.id,
            date: item.date,
            name: item.experienceName,
            content: item.content,
            status: parseInt(item.status)
        }))
    } catch (error) {
        ElMessage.error('获取经历列表失败')
    } finally {
        loading.value = false
    }
}

onMounted(() => {
    fetchExperiences()
})

const dialogVisible = ref(false)
const dialogTitle = ref('新增经历')
const isEditing = ref(false)
const editingIndex = ref(null)
const currentExperience = reactive({
    id: null,
    date: '',
    name: '',
    content: '',
    status: '1'
})

const showAddDialog = () => {
    dialogTitle.value = '新增经历'
    Object.assign(currentExperience, {
        id: null,
        date: '',
        name: '',
        content: '',
        status: '1'
    })
    isEditing.value = false
    dialogVisible.value = true
}

const editExperience = (index) => {
    dialogTitle.value = '编辑经历'
    Object.assign(currentExperience, { ...experiences.value[index] })
    currentExperience.status = currentExperience.status.toString()
    isEditing.value = true
    editingIndex.value = index
    dialogVisible.value = true
}

const saveExperience = async () => {
    if (!currentExperience.date || !currentExperience.name) {
        ElMessage.error('请填写完整信息')
        return
    }

    try {
        const params = {
            date: currentExperience.date,
            experienceName: currentExperience.name,
            content: currentExperience.content,
            status: currentExperience.status.toString()
        }

        if (isEditing.value) {
            params.id = currentExperience.id
            await updateExperience(params)
            ElMessage.success('更新成功')
        } else {
            await addExperience(params)
            ElMessage.success('新增成功')
        }

        dialogVisible.value = false
        await fetchExperiences()
    } catch (error) {
        ElMessage.error(isEditing.value ? '更新失败' : '新增失败')
    }
}

const toggleStatus = async (index) => {
    const params = {
        id: experiences.value[index].id,
        date: experiences.value[index].date,
        experienceName: experiences.value[index].name,
        content: experiences.value[index].content,
        status: experiences.value[index].status == '1' ? '0' : '1'
    }
    await updateExperience(params)
    ElMessage.success('状态已更新')
    await fetchExperiences()
}

const deleteExperience = async (index) => {
    try {
        await ElMessageBox.confirm('确定要删除该经历吗?', '提示', {
            confirmButtonText: '确定',
            cancelButtonText: '取消',
            type: 'warning'
        })
        await delExperience({ id: experiences.value[index].id })
        await fetchExperiences()
        ElMessage.success('删除成功')
    } catch (error) {
        if (error !== 'cancel') {
            ElMessage.error('删除失败')
        }
    }
}
</script>

<style scoped>
.experience-management {
    padding: 24px;
    min-height: calc(100vh - 64px);
}

.header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 24px;
    padding: 16px 24px;
    background: white;
    border-radius: 8px;
    box-shadow: 0 1px 4px rgba(0, 0, 0, 0.08);
}

.header h2 {
    margin: 0;
    font-size: 20px;
    color: #303133;
}

.experience-list {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(400px, 1fr));
    gap: 24px;
}

.experience-card {
    transition: all 0.3s cubic-bezier(0.25, 0.8, 0.25, 1);
    border-radius: 8px;
    overflow: hidden;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.12);
}

.experience-card:hover {
    transform: translateY(-5px);
    box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
}

.experience-card.disabled {
    opacity: 0.6;
    filter: grayscale(50%);
}

.card-content {
    padding: 16px;
}

.experience-info {
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.experience-date {
    margin-bottom: 8px;
}

.experience-info h3 {
    margin: 0;
    font-size: 16px;
    font-weight: 500;
    color: #303133;
}

.experience-content {
    margin: 8px 0;
    font-size: 14px;
    color: #606266;
    line-height: 1.6;
}

.status {
    margin-top: 12px;
}

.card-actions {
    display: flex;
    justify-content: space-between;
    padding: 12px 16px;
    background-color: #f9fafc;
    border-top: 1px solid #ebeef5;
}

.card-actions .el-button {
    flex: 1;
    margin: 0 4px;
}

@media (max-width: 768px) {
    .experience-list {
        grid-template-columns: 1fr;
    }

    .card-actions {
        flex-direction: column;
        gap: 8px;
    }

    .card-actions .el-button {
        width: 100%;
        margin: 0;
    }
}

@keyframes fadeIn {
    from {
        opacity: 0;
        transform: translateY(10px);
    }

    to {
        opacity: 1;
        transform: translateY(0);
    }
}

.experience-card {
    animation: fadeIn 0.4s ease-out;
}
</style>
